//
//  BaseViewController.m
//  HuiLeBao
//
//  Created by 杨运涛 on 15/11/23.
//  Copyright © 2015年 AnHuiJie. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController () <UITableViewDataSource, UITableViewDelegate>

///画圆角和边框的视图
@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *pArrayBorderAndRadius;

@end

@implementation BaseViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}


#pragma mark - 初始化时使用的方法

///初始化变量或界面使用的方法
- (void)initInfo {
    if (!self.pTableView.tableFooterView) {
        [self.pTableView setTableFooterView:[UIView new]];
    }
}

///设置背景颜色
- (void)selDefaultNavigationBackground {
    //顶部状态栏图片颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:102/255.0f green:102/255.0f blue:102/255.0f alpha:1.0f];
}

///设置标题
- (void)selDefaultNavigationWithTitle:(NSString *)strTitle {
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [UIFont boldSystemFontOfSize:20], NSFontAttributeName, nil];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.title = strTitle;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1]];
    
    for (UIView *viewItem in self.pArrayBorderAndRadius)
    {
        [viewItem.layer setCornerRadius:2.0f];
        [viewItem.layer setMasksToBounds:YES];
    }
    
    [self initInfo];
}

///pop或者dismiss
- (void)selPopOrDismissViewController
{
    @try {
        if (self.navigationController == nil || self.navigationController.viewControllers.count == 1) {
            [self dismissViewControllerAnimated:YES completion:^{}];
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    @catch (NSException *exception) {}
    @finally {}
}

///PopToRootViewController
- (void)selPopToRootViewController {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

///内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)selAddLeftBackButton
{
    UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    [btnBack setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [btnBack setTitle:@"   " forState:UIControlStateNormal];
    [btnBack.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    [btnBack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnBack setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [btnBack addTarget:self action:@selector(selPopOrDismissViewController) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem = leftBarItem;
}

- (void)selAddRightButton:(NSString *)strTitle imageName:(NSString *)strImageName selector:(SEL)theSelector
{
    UIButton *btnRight = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    if (strTitle.length > 0) {
        NSDictionary *attrs = @{NSFontAttributeName : btnRight.titleLabel.font};
        CGSize size = [strTitle boundingRectWithSize:CGSizeMake(1000, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
        if (size.width < 50) {
            [btnRight setFrame:CGRectMake(0, 0, 50, 30)];
        } else if (size.width > 100) {
            [btnRight setFrame:CGRectMake(0, 0, 100, 30)];
        } else {
            [btnRight setFrame:CGRectMake(0, 0, size.width, 30)];
        }
        
        [btnRight setTitle:strTitle forState:UIControlStateNormal];
        [btnRight.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
        [btnRight setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnRight setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    }
    if (strImageName.length > 0) {
        @autoreleasepool {
            UIImage *imgBtn = [UIImage imageNamed:strImageName];
            [btnRight setImage:imgBtn forState:UIControlStateNormal];
            [btnRight.imageView setContentMode:UIViewContentModeScaleAspectFit];
            btnRight.imageEdgeInsets = UIEdgeInsetsMake(0, btnRight.frame.size.width - btnRight.frame.size.height, 0, 0);
        }
    }
    [btnRight addTarget:self action:theSelector forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:btnRight];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}

- (void)selSuperView:(UIView *)superView SetSubLabelOfTag:(int)intTag andValue:(NSString *)strValue {
    UILabel *lbl = (UILabel *)[superView viewWithTag:intTag];
    @try {
        [lbl setText:strValue];
    }
    @catch (NSException *exception) {}
    @finally {}
}

- (void)selInform:(NSString *)associationObjID passiveObjID:(NSString *)passiveObjID appTime:(NSString *)appTime andContent:(NSString *)strContent
{
    [SVProgressHUD show];
    [TDNetwork inform:inform_interface associationObj:associationObjID passiveObj:passiveObjID appTime:appTime complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        [SVProgressHUD showSuccessWithStatus:@"举报信息已经提交,我们将尽快处理,感谢您的支持."];
        [self performSelector:@selector(selPopToRootViewController) withObject:nil afterDelay:1.5f];
    }];
}


#pragma mark - 关闭键盘

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[UITableViewCell alloc] init];
}

@end
